package com.haitaoit.chengquan.module.home.adapter.parts;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BackCall;
import com.haitaoit.chengquan.module.home.model.PartsModel;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by Administrator on 2016/10/24.
 */
public class PartsListAdapter extends BaseAdapter {
    private Context context;
    private BackCall backCall;
    private List<PartsModel> list;

    public void setBackCall(BackCall backCall) {
        this.backCall = backCall;
    }

    public PartsListAdapter(Context context) {
        this.context = context;
    }

    public List<PartsModel> getList() {
        return list;
    }

    public void setList(List<PartsModel> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.parts_list_item, null);
            holder = new ViewHolder();
            ViewUtils.inject(holder, convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.part_list_itemll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PartsListAdapter.this.backCall.deal(R.id.part_list_itemll, position);
            }
        });

        ImageLoader.getInstance().displayImage(list.get(position).getTitle_img(), holder.parts_list_img);
        holder.part_title.setText(list.get(position).getTitle());
        holder.part_price.setText("¥ " + list.get(position).getPrice());
        holder.sales_volume.setText(list.get(position).getSales_volume()+context.getString(R.string.PartsActivity_already_buy));



        holder.parts_list_imgtype.setVisibility(View.VISIBLE);
        // type:类型1无2热门3最新
        if (list.get(position).getType().equals("1")) {
            holder.parts_list_imgtype.setVisibility(View.GONE);
        } else if (list.get(position).getType().equals("2")) {
            holder.parts_list_imgtype.setImageResource(R.mipmap.img31);
        } else if (list.get(position).getType().equals("3")) {
            holder.parts_list_imgtype.setImageResource(R.mipmap.img32);
        }
        return convertView;
    }

    class ViewHolder {
        @ViewInject(R.id.part_list_itemll)
        LinearLayout part_list_itemll;
        @ViewInject(R.id.parts_list_img)
        ImageView parts_list_img;
        @ViewInject(R.id.parts_list_imgtype)
        ImageView parts_list_imgtype;
        @ViewInject(R.id.part_title)
        TextView part_title;
        @ViewInject(R.id.part_price)
        TextView part_price;
        @ViewInject(R.id.sales_volume)
        TextView sales_volume;
    }
}
